import React, {useState} from 'react';
import AuthComponent from '@/components/Authorized/AuthComponent';
import { Collapse, Input, Button, Switch,Modal,message,Row, Col } from 'antd';
import { Form } from '@ant-design/compatible';
import styles from './index.less';
import DynamicFieldSet from './CollapseAccordion/components/FormDynamic.jsx';
import { useRequest } from '@umijs/hooks';
import {getWebConfigInfo , saveOverBookConfig} from './service';
import { createForm } from 'rc-form'
const { confirm } = Modal
const { Panel } = Collapse;
const CollapseAccordion = props => {
    const { form } = props
    //请求初始化页面
    const { data: detailsdata={timeDifferenceInfo:[],refundInfo:{}}, loading: detailLoading } = useRequest(() => getWebConfigInfo(''))
    const [visible, handleVisible] = useState(false)
    // 保存
     const okHandle = () => {
      handleVisible(true)
      form.validateFields({ force: true }, (error) => {
        if (!error){
          confirm({
            title: '提示',
            okText:'确认',
            cancelText:'取消',
            visible: visible,
            content: '确认操作后，将修改旅客超售设置表，修改之后无法撤回，你还要继续吗？',
            onOk: () => {
              return new Promise((resolve, reject) => {
                form.validateFields((err, fieldsValue) => {
                  let timeDifferenceInfoData = []
                  props.form.getFieldsValue().params.forEach((item, index) => {
                    item.payType = '0'
                    timeDifferenceInfoData.push(item)
                  })
                  let res = resolve(saveOverBookConfig({
                    'refundInfo':{
                      payMoney: fieldsValue.refundInfoconditionMoney,
                      payType: '1',
                    },
                    'timeDifferenceInfo': timeDifferenceInfoData
                  }))
                  message.success('保存成功')
                });
                  
              })
            }
           })
        }else{
          message.error('设置表单填写数据错误，请填写正确格式')
        }
      })
      }
    return !detailLoading&&(
       <div className={styles.container}>
         <div id="components-collapse-demo-accordion" className={styles.formStyle}>
           {/* <Collapse accordion defaultActiveKey={['1']}> */}
           <div>
              {/* <Panel header="超售退票" key="1"> */}
              <div className={styles.item}>
                 <div className={styles.title}><b>退票补偿标准</b></div>
                 <div className={styles.form}>
                    <Row type="flex" justify="start">
                      <Col>
                        <Form.Item>
                          旅客退票时，不计算购票金额，均补偿
                          {form.getFieldDecorator('refundInfoconditionMoney', {
                            initialValue: detailsdata && detailsdata.refundInfo && detailsdata.refundInfo.payMoney ? detailsdata.refundInfo.payMoney : '',
                            rules: [{
                              pattern: /^([1-9][0-9]*)+$/,
                              message: '只能大于0的整数'
                            }],
                          })(<Input style={{ width: 70, margin:'0 10px'}} />)}元
                        </Form.Item>
                      </Col>
                    </Row>
                 </div>
               </div>
             {/* </Panel> */}
             {/* <Panel header="超售改签" key="2"> */}
               <div className={styles.item}>
                 <div className={styles.title}><b>改签补偿标准</b></div>
                 <div className={styles.form}>
                    <DynamicFieldSet timeDifferenceInfoData={detailsdata&&detailsdata.timeDifferenceInfo?detailsdata.timeDifferenceInfo:[]} form={form}/>
                 </div>
               </div>
             {/* </Panel> */}
             </div>

           {/* </Collapse> */}
         </div>
         <p style={{display:'flex',marginLeft:'48%'}}>
        <AuthComponent authCode="overSale.setting.submit">
          <Button type="primary" onClick={okHandle}>
            提交
          </Button>
        </AuthComponent>
         </p>
       </div>
    )

  }

  export default Form.create()(CollapseAccordion);
